const mongoose = require('mongoose');

// 建立链接
mongoose.connect('mongodb://127.0.0.1:27017/class0331');
// 打开链接
mongoose.connection.on('open',()=>{
    console.log('数据库链接成功');
})
mongoose.connection.on('error',()=>{
    console.log('数据库链接失败');
})

// 创建schema
const noteSchema = new mongoose.Schema({
    title:String,
    content:String
})
// 创建模型
const noteModel = mongoose.model('notes',noteSchema);
// 查询数据库
let p = new Promise((resolve,reject)=>{
    noteModel.find({},(err, data)=>{
        if(err){
            reject(err.message);
        }
        resolve(data);
    })
})

p.then(data=>{
    console.log('data',data);
}, reason=>{
    console.log('失败: ' + reason);
})


